package com.twu.biblioteca;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: luisarmando
 * Date: 1/25/13
 * Time: 11:15 AM
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaController {
    private Scanner scanner;
    private PrintWriter out;
    public List<String> menus;
    private List<Book> bookList;
    private User logged_user;

    public BibliotecaController(Scanner scanner, PrintWriter out){
        this.scanner = scanner;
        this.out = out;
        this.prepareMenus();
        this.bookList = new ArrayList<Book>();
        this.logged_user = null;
    }

    public boolean isLoggedIn(){
        return this.logged_user!=null;
    }

    protected void prepareMenus(){
        menus = new ArrayList<String>();
        menus.add("View List of Books");
        menus.add("Reserve Book");
        menus.add("Membership Details");
        menus.add("View List of Movies");
        if (!isLoggedIn()){
            menus.add("Login");
        }
    }

    public List<String> getMenus() {
        return menus;
    }

    public List<Book> getBookList() {
        return bookList;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }

    public void booksList(){
        for (int i=0; i<this.bookList.size(); i++){
            this.out.println(Integer.toString(i+1) + ". " + bookList.get(i));
        }
    }

    public void reserveBook(){
        if (!isLoggedIn()){
            this.out.print("You must be logged in!");
            return;
        }
        this.out.print("Select the book you want to reserve: ");
        if (!this.scanner.hasNext())
            return;
        int selected = this.scanner.nextInt();
        if (selected <= 0 || selected>this.getBookList().size())
            return;
        Book book = this.getBookList().get(selected-1);
        if (!book.isReserved()){
            this.out.println("Thank You! Enjoy the book.");
            book.setReserved(true);
        } else {
            this.out.println("Sorry we don't have that book yet.");
        }
    }

    private void membershipDetails() {
        if (!isLoggedIn()){
            this.out.println("Please talk to a Librarian. Thank you.");
        } else {
            this.out.println(this.logged_user.toString());
        }
    }

    private void moviesList() {
        this.out.println("Movie\tYear\tDirector\tRating");
        List<Movie> list = Movie.getMovies();
        for (Movie movie : list){
            this.out.println(movie);
        }
    }

    private void login() {
        this.out.println("Type your login: ");
        if (!this.scanner.hasNext())
            return;
        String login = scanner.next();
        this.out.println("Type your password: ");
        if (!this.scanner.hasNext())
            return;
        String password = scanner.next();
        this.logged_user = User.authenticate(login, password);
        if (isLoggedIn()){
            this.out.println("Successfuly logged in");
        } else {
            this.out.println("Login error");
        }
    }

    public void handleMenuSelection(int menuOpt) {
        if (menuOpt <= 0 || menuOpt > this.getMenus().size()){
            this.out.println("Select a valid option!!");
            return ;
        }
        this.out.println("You selected \""+this.getMenus().get(menuOpt-1)+"\"");
        switch (menuOpt){
            case 1 :
                this.booksList();
                break;
            case 2 :
                this.reserveBook();
                break;
            case 3:
                this.membershipDetails();
                break;
            case 4:
                this.moviesList();
                break;
            case 5:
                if (!isLoggedIn()){
                    this.login();
                }
                break;
            default:
                break;
        }
    }
}
